package com.zkingsoft.services.pc.impl;

import com.zkingsoft.services.pc.PcMealSettingsService;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.bus.BusParameterSettings;
import com.zkingsoft.model.pc.PcMealSettings;
import com.zkingsoft.dao.bus.BusParameterSettingsDao;
import com.zkingsoft.dao.pc.PcMealSettingsDao;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Map;

import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.util.ModelUtils;
import com.zkingsoft.util.WebUtil;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-03-23 14:19
 */
@Service("pcMealSettingsService")
public class  PcMealSettingsServiceImpl implements    PcMealSettingsService  {
	Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private PcMealSettingsDao pcMealSettingsDao;
	@Autowired
	private BusParameterSettingsDao busParameterSettingsDao;
	
	@Transactional
	@Override
	public int add(PcMealSettings pcMealSettings){
		upDateBus(pcMealSettings);
		int i=0;
		List<PcMealSettings> pcMeals = pcMealSettings.getPcMeals();
		pcMealSettingsDao.deleteByModel(null);
    	for(PcMealSettings pcm1:pcMeals){
    		if(pcm1!=null && pcm1.getMesMeal()!=null){
    		i+=pcMealSettingsDao.insert(pcm1);
    		}
    	}
		return i;
	}
	/**
	 * 更新就餐人数
	 */ 
	public void upDateBus(PcMealSettings pcMealSettings){
		BusParameterSettings bus=new BusParameterSettings();
		bus.setParamCode("rs");
		List<BusParameterSettings> busList=busParameterSettingsDao.selectByModel(bus);
		for(BusParameterSettings b:busList){
			Long id=b.getParamId();
			bus.setParamId(id);
		}
		bus.setParamValue(pcMealSettings.getParamValue());
		busParameterSettingsDao.updateByModel(bus);
	}
	@Override
	public int batchAdd(List<PcMealSettings>  pcMealSettingsList) {
		int num = 0;

		int c = 10000;
		int size = pcMealSettingsList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= pcMealSettingsList.size() ? pcMealSettingsList.size() : end;
			List<PcMealSettings> insertList = pcMealSettingsList.subList(begin, end);
			num += pcMealSettingsDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(PcMealSettings oldPcMealSettings
	,PcMealSettings newPcMealSettings){
		if (!ModelUtils.isModified(oldPcMealSettings, newPcMealSettings)) {
			return 1;
		}
		Map<String, Object> modifyMap=null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldPcMealSettings, newPcMealSettings);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("mesId", oldPcMealSettings.getMesId());
			pcMealSettingsDao.updateByMap(modifyMap);
		}
		return 1;
	}
	
	@Override
	public int modifyByModel(PcMealSettings pcMealSettings){
	
		return pcMealSettingsDao.updateByModel(pcMealSettings);
	
	}
	
	
	
	@Override
	public int remove(List<Long> list){
	
		return pcMealSettingsDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(Long mesId){
	
		return pcMealSettingsDao.deleteById(mesId);
	
	}
	
	@Override
	public int removeByModel(PcMealSettings pcMealSettings){
	
		return pcMealSettingsDao.deleteByModel(pcMealSettings);
	
	}
	
	
	@Override
	public List<PcMealSettings> findInPage(PcMealSettings pcMealSettings,  PaginationVO pageVo){
	
		return pcMealSettingsDao.selectInPage(pcMealSettings , pageVo);
	
	}
	
	@Override
	public List<PcMealSettings> findByModel(PcMealSettings pcMealSettings){
	
		return pcMealSettingsDao.selectByModel(pcMealSettings);
	
	}
	
	@Override
	public int  findTotal(PcMealSettings pcMealSettings){
	
		return pcMealSettingsDao.selectTotalRecord(pcMealSettings);
	
	}
	
	@Override
	public PcMealSettings  findById(Long mesId){
	
		return pcMealSettingsDao.selectById(mesId);
	
	}

   	
	
	
}